package com.lanhai.zhonglai.dispatch;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.lanhai.zhonglai.enums.StationDataTypeEnum;


/**
 * 机台状态缓存，高频，不需要写redis
 *
 * @author zhengwei
 */
public class StationCmdMap {
    static Map<Integer, String> cmdMap = new ConcurrentHashMap<Integer, String>();

    /*
     *  三种状态理论一个map就能处理，但PLC的指令并不友好
     *  高频处理，不需要写redis
     */
    static Map<Integer, String> transMap = new ConcurrentHashMap<Integer, String>();
    static Map<Integer, String> leaveMap = new ConcurrentHashMap<Integer, String>();
    static Map<Integer, String> receiveMap = new ConcurrentHashMap<Integer, String>();

    public static void save(int dotno, String cmd) {
        cmdMap.put(dotno, cmd);

    }

    public static boolean isSame(int dotno, String cmd) {
        String tmd = cmdMap.get(dotno);
        if (tmd != null && tmd.equals(cmd)) {
            return true;
        } else {
            return false;
        }
    }

    //传输相关
    public static void saveState(int dotno, StationDataTypeEnum type) {
        switch (type) {
            case AllowLeave:
                leaveMap.put(dotno, "1");
                break;
            case AllowTrans:
                transMap.put(dotno, "1");
                break;
            case Receive:
                receiveMap.put(dotno, "1");
                break;
            default:
                break;

        }
    }


    public static void stopTrans(int dotno) {
        transMap.put(dotno, "");
    }


    /**
     * 允许传输
     *
     * @param dotno
     * @return
     */
    public static boolean canTrans(int dotno) {
        String k = transMap.get(dotno);
        if (k != null && k.equals("1")) {
            return true;
        } else {
            return false;
        }

    }


    /**
     * 准备转动
     *
     * @param dotno
     * @return
     */
    public static boolean canReceive(int dotno) {
        String k = receiveMap.get(dotno);
        if (k != null && k.equals("1")) {
            return true;
        } else {
            return false;
        }
    }


    /**
     * 允许离开
     *
     * @param dotno
     * @return
     */
    public static boolean canLeave(int dotno) {
        String k = leaveMap.get(dotno);
        if (k != null && k.equals("1")) {
            return true;
        } else {
            return false;
        }
    }

    public static void clearState(int dotno) {
        leaveMap.put(dotno, "");
        transMap.put(dotno, "");
        receiveMap.put(dotno, "");
    }
}
